﻿#include "OmaKing.h"
#include "../../../Shared/Functions/Functions.h"
#include "../../../Shared/Enums.h"
#include "../MapObject.h"
#include "../../MirEnvir/Envir.h"
#include "../../../Shared/ServerPackets.h"
#include "../../../Shared/Data/Stat.h"
#include "../../Settings.h"
#include "../DelayedAction.h"

using namespace Server::MirDatabase;
using namespace Server::MirEnvir;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    OmaKing::OmaKing(MonsterInfo *info) : MonsterObject(info)
    {
    }

    bool OmaKing::InAttackRange()
    {
        if (getTarget()->getCurrentMap() != getCurrentMap())
        {
            return false;
        }
        if (getTarget()->getCurrentLocation() == getCurrentLocation())
        {
            return false;
        }

        int x = std::abs(getTarget()->getCurrentLocation()->X - getCurrentLocation()->X);
        int y = std::abs(getTarget()->getCurrentLocation()->Y - getCurrentLocation()->Y);

        if (x > 2 || y > 2)
        {
            return false;
        }


        return (x <= 1 && y <= 1) || (x == y || x % 2 == y % 2);
    }

    bool OmaKing::InRangedAttackRange()
    {
        return getCurrentMap() == getTarget()->getCurrentMap() && Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), AttackRange);
    }

    void OmaKing::CompleteAttack(std::vector<std::any> &data)
    {
        int damage = std::any_cast<int>(data[1]);
        DefenceType defence = std::any_cast<DefenceType>(data[2]);

        std::vector<MapObject*> targets = FindAllTargets(AttackRange, getCurrentLocation());
        if (targets.empty())
        {
            return;
        }

        for (int i = 0; i < targets.size(); i++)
        {
            setTarget(targets[i]);

            if (getTarget() == nullptr || !getTarget()->IsAttackTarget(this) || getTarget()->getCurrentMap() != getCurrentMap() || getTarget()->Node == nullptr)
            {
                continue;
            }

            getTarget()->Attacked(this, damage, defence);
        }
    }

    void OmaKing::Attack()
    {
        if (!getTarget()->IsAttackTarget(this))
        {
            setTarget(nullptr);
            return;
        }

        ShockTime = 0;

        setDirection(Functions::DirectionFromPoint(getCurrentLocation(), getTarget()->getCurrentLocation()));
        bool ranged = getCurrentLocation() == getTarget()->getCurrentLocation() || !InAttackRange();

        ActionTime = getEnvir()->getTime() + 300;
        AttackTime = getEnvir()->getTime() + AttackSpeed;

        int damage;

        if (!ranged && getEnvir()->Random->Next(3) > 0)
        {
            S::ObjectAttack *tempVar = new S::ObjectAttack();
            tempVar->ObjectID = ObjectID;
            tempVar->Direction = getDirection();
            tempVar->Location = getCurrentLocation();
            Broadcast(tempVar);
            damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);
            if (damage == 0)
            {
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
                return;
            }

            std::vector<MapObject*> targets = FindAllTargets(1, getCurrentLocation());
            if (targets.empty())
            {
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
                return;
            }

            int levelgap;

            for (int i = 0; i < targets.size(); i++)
            {
                if (targets[i]->IsAttackTarget(this))
                {
                    levelgap = 60 - targets[i]->getLevel();
                    if (getEnvir()->Random->Next(20) < 4 + levelgap)
                    {
                        if (getEnvir()->Random->Next(Settings::MagicResistWeight) < targets[i]->Stats[Stat::MagicResist])
                        {
                            continue;
                        }
                        if (targets[i]->Pushed(this, Functions::DirectionFromPoint(getCurrentLocation(), targets[i]->getCurrentLocation()), 3 + getEnvir()->Random->Next(3)) > 0 && getEnvir()->Random->Next(8) == 0)
                        {
                            if (getEnvir()->Random->Next(Settings::PoisonResistWeight) >= targets[i]->Stats[Stat::PoisonResist])
                            {
                                Poison *tempVar2 = new Poison(), *this, *true;
                                tempVar2->PType = PoisonType::Paralysis;
                                tempVar2->Duration = 5;
                                tempVar2->TickSpeed = 1000;
                                targets[i]->ApplyPoison(tempVar2, this, true);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar2' statement was not added since tempVar2 was passed to a method or constructor. Handle memory management manually.
                            }
                        }
                    }
                }
            }

            LineAttack(2, 300, DefenceType::ACAgility);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
        }
        else
        {
            AttackTime = getEnvir()->getTime() + AttackSpeed + 500;
            S::ObjectAttack *tempVar3 = new S::ObjectAttack();
            tempVar3->ObjectID = ObjectID;
            tempVar3->Direction = getDirection();
            tempVar3->Location = getCurrentLocation();
            tempVar3->Type = 1;
            Broadcast(tempVar3);

            damage = GetAttackPower(Stats[Stat::MinMC], Stats[Stat::MaxMC]);
            if (damage == 0)
            {
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar3' statement was not added since tempVar3 was passed to a method or constructor. Handle memory management manually.
                return;
            }

            DelayedAction *action = new DelayedAction(DelayedType::Damage, getEnvir()->getTime() + 500, {getTarget(), damage, DefenceType::MAC});
            ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar3' statement was not added since tempVar3 was passed to a method or constructor. Handle memory management manually.
        }
    }

    void OmaKing::ProcessTarget()
    {
        if (getTarget() == nullptr)
        {
            return;
        }

        if (InRangedAttackRange() && getCanAttack())
        {
            Attack();
            return;
        }

        if (getEnvir()->getTime() < ShockTime)
        {
            setTarget(nullptr);
            return;
        }

        MoveTo(getTarget()->getCurrentLocation());
    }
}
